﻿// -----------------------------------------------------------------------
// <copyright file="MetalListBox.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Metal.Config.ExtendedControls
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class MetalListView : ListView
    {
        private bool canCheck;
        private bool isChecking;

        /// <summary>Stores the attribute / setting name, where the value of the check box will be saved</summary>
        private string internalValue;

        protected override void OnMouseClick(MouseEventArgs e)
        {
            base.OnMouseClick(e);

            if (!this.GetItemAt(e.X, e.Y).Checked)
            {
                canCheck = true;
                this.GetItemAt(e.X, e.Y).Checked = true;
            }
        }

        /// <summary>
        /// Save CheckBox value
        /// </summary>
        public void SaveSetting()
        {
            Settings.Instance.SetValue("LastSettings.Theme" + this.SettingName, this.SelectedItems[0].ToString());
        }

        /// <summary>
        /// Gets or sets the setting name
        /// </summary>
        public string SettingName
        {
            get
            {
                return this.internalValue;
            }

            set
            {
                this.internalValue = value;
            }
        }

        protected override void OnItemCheck(ItemCheckEventArgs e)
        {
            base.OnItemCheck(e);

            if (!isChecking && canCheck)
            {
                isChecking = true;
                foreach (ListViewItem item in this.Items)
                {
                    item.Checked = false;
                }
                this.Items[e.Index].Checked = true;
                e.NewValue = CheckState.Checked;
                canCheck = false;
                isChecking = false;
            }
            else
            {
                if (isChecking)
                {
                    e.NewValue = CheckState.Unchecked;
                }
                else
                {
                    e.NewValue = e.CurrentValue;
                    //Now I have a method here, when the new tick is checked!
                }
            }
        }

        /// <summary>
        /// When the control becomes visible load the control value (checked / unchecked) and the text (resource)
        /// </summary>
        /// <param name="e">Event args</param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);

            if (this.SettingName != null)
            {
                string themeName = Settings.Instance.GetValue("LastSettings." + this.SettingName, string.Empty);
                this.Items[themeName].Checked = true;
            }
        }
    }
}
